package com.im.app.controller;

import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.Template;
import com.im.app.model.User;
import com.im.app.service.ITemplateService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/template")
public class TemplateController extends BaseController
{
    @Resource
    private ITemplateService templateService;


    @GetMapping(value = "/queryTemplateList")
    @RequireLogin
    public ResponseResult<List<Template>> queryTemplateList(@CurrentUser User user) {
        //调用业务层返回列表
        Template Template = new Template();
        Template.setUserId(user.getUserId());
        List<Template> TemplateList = templateService.selectTemplateList(Template);
        return Response.makeOKRsp(TemplateList);
    }

    @GetMapping(value = "/getTemplates")
    public ResponseResult<List<Template>> getTemplates(String userId) {
        //调用业务层返回列表
        Template Template = new Template();
        Template.setUserId(userId);
        List<Template> TemplateList = templateService.selectTemplateList(Template);
        return Response.makeOKRsp(TemplateList);
    }


    @GetMapping(value ="/find")
    @RequireLogin
    public ResponseResult<Template> findTemplate(Integer id) {
        try {
            //调用业务层返回列表
            Template Template = templateService.selectTemplateById(Long.valueOf(id));
            return Response.makeOKRsp(Template);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询模板信息异常");
        }
    }

    @GetMapping(value ="/delete")
    @RequireLogin
    public ResponseResult deleteTemplate(Integer id) {
        int result = templateService.deleteTemplateById(Long.valueOf(id));
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }

    }

    @PostMapping(value ="/add")
    @RequireLogin
    public ResponseResult<Object> addTemplate(@CurrentUser User user, @RequestBody Template Template) {
        Template.setUserId(user.getUserId());
        int result = templateService.insertTemplate(Template);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @PostMapping(value ="/update")
    @RequireLogin
    public ResponseResult updateTemplate(@CurrentUser User user,@RequestBody Template Template) {
        Template.setUserId(user.getUserId());
        int result = templateService.updateTemplate(Template);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }
}
